import numpy as np
import csv


def load_data(file_path):
    """
    Load CSV data into NumPy array
    Columns: student_id, name, math_score, english_score, python_score
    Return: 2D NumPy array of shape (students, 3) containing scores
    """
    with open(file_path, mode='r') as file:
        reader = csv.DictReader(file)
        data = []
        for row in reader:
            print(row)
            
            math_score = float(row['math_score'])
            english_score = float(row['english score'])
            python_score = float(row['python score'])
            data.append([math_score, english_score, python_score])
    return np.array(data)


def calculate_statistics(data):
    """
    Calculate required statistics for each subject
    Return: Dictionary containing {
        'means': [math_mean, english_mean, python_mean],
        'medians': [math_median, english_median, python_median],
        'variances': [math_var, english_var, python_var],
        'stds': [math_std, english_std, python_std]
    }
    """
    means = np.round(np.mean(data, axis=0), 1)
    medians = np.round(np.median(data, axis=0), 1)
    variances = np.round(np.var(data, axis=0), 1)
    stds = np.round(np.std(data, axis=0), 1)

    return {
        'means': means,
        'medians': medians,
        'variances': variances,
        'stds': stds
    }


def print_results(stats):
    """
    Print results in EXACTLY this format:
    Math:
        Average: 85.0
        Median: 86.0
        Variance: 25.0
        Standard Deviation: 5.0
    English:
        ...
    Best Subject: Math
    Most Consistent Subject (Lowest Std): English (4.2)
    """
    subjects = ['Math', 'English', 'Python']
    for i, subject in enumerate(subjects):
        print(f"{subject}:")
        print(f"    Average: {stats['means'][i]}")
        print(f"    Median: {stats['medians'][i]}")
        print(f"    Variance: {stats['variances'][i]}")
        print(f"    Standard Deviation: {stats['stds'][i]}")

    best_subject = subject[np.argmin(stats['means'])]
    most_consistent_subject = subjects[np.argmin(stats['stds'])]
    most_consistent_std = stats['stds'][np.argmin(stats['stds'])]

    print(f"Best Subject: {best_subject}")
    print(f"Most Consistent Subject (Lowest Std): {most_consistent_subject} ({most_consistent_std})")


def main():
    data = load_data('scores.csv')

    stats = calculate_statistics(data)

    print_results(stats)


if __name__ == "__main__":
    main()